﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;

public partial class search_sale : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Init.islogin(ref UserID,ref EnterpriseName,ref UserPower))
        {
            Response.Redirect("/Login.aspx");
        }
        this.EnableViewState = false;
        Response.Write(Init.InitIndex(EnterpriseName, UserPower));
        Footer = Init.IndexFooter();
        srq = Request.QueryString["srq"];
        erq = Request.QueryString["erq"];
        ckdh = Request.QueryString["ckdh"];
        if (!string.IsNullOrEmpty(srq) || !string.IsNullOrEmpty(ckdh) || !string.IsNullOrEmpty(erq))
        {
            DataSet ds = DataGroup(ckdh, srq,erq);
            GridView1.DataSource = ds;
            GridView1.DataKeyNames = new string[] { "djbh" };//主键
            int recordCount = ds.Tables[0].Rows.Count;
            GridView1.DataBind();
        }        
    }

    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    public string srq = "";
    public string erq = "";
    public string ckdh = "";
    
    //分页
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = DataGroup(ckdh, srq, erq);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = ds.Tables[0].Rows.Count;
        GridView1.DataBind();
    }

    //展示
    public DataSet DataGroup(string ckdh,string startrq="",string endrq="")
    {
        EnterpriseName = (UserPower.Contains("Manager")) ? "" : EnterpriseName;
        string sql = "select a.rq,a.djbh,a.zhy,b.dwmch,count(*) as pzs,"
            + "'invoice2.ashx?ent=" + EnterpriseName + "&djbh='+rtrim(a.djbh)+'&bg=true' as bgurl, "
            + "'invoice2.ashx?ent=" + EnterpriseName + "&djbh='+rtrim(a.djbh)+'&pj=true' as pjurl, "
            + "'invoice2.ashx?ent=" + EnterpriseName + "&djbh='+rtrim(a.djbh)+'&sm=true' as zlurl, ";
        if (UserPower.Contains("invoice"))
        {
            sql += "'invoice2.ashx?ent=" + EnterpriseName + "&djbh='+rtrim(a.djbh)+'&fp=true&shd=true' as lpurl ";
        }
        else
        {
            sql += "'' as lpurl ";
        }
        sql += " from [spls_ck] a(nolock),[mchk] b(nolock) where a.dwbh=b.dwbh ";
        sql += (UserPower.Contains("Manager")) ? "" : " and b.dwmch='" + EnterpriseName + "'";
       // sql+=(UserPower.Contains("Manager"))?" 1=1 ":"dwbh in(select dwbh from [mchk](nolock) where dwmch='" + EnterpriseName + "')";
        if (!string.IsNullOrEmpty(ckdh))
        {
            sql += " and left(a.djbh,14)='" + ckdh + "' ";
        }
        if (!string.IsNullOrEmpty(startrq))
        {
            sql += " and rq>='" + startrq + "' ";
        }
        if (!string.IsNullOrEmpty(endrq))
        {
            sql += " and rq<='" + endrq + "' ";
        }
        sql += " group by rq,djbh,zhy,b.dwmch order by djbh desc,rq desc";
        if (Init.IsDebug)
        {
            ResultDeBug.Text = sql;
        }
        DataSet dss = mssql.ExecuteDs(sql);
        return dss;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        DataSet ds = DataGroup(ckdh);
        GridView1.DataSource = ds;
        GridView1.DataKeyNames = new string[] { "djbh" };//主键
        int recordCount = ds.Tables[0].Rows.Count;
        GridView1.DataBind();
    }   
}